package com.henme.crm.web.product;

import com.henme.crm.entity.product.ProductCategory;
import com.henme.crm.service.product.ProductCategoryService;
import com.henme.crm.util.dwz.AjaxDone;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/category")
public class ProductCategoryController {
    private static final Logger logger = LoggerFactory.getLogger(ProductCategoryController.class);

    @Autowired
    private ProductCategoryService productCategoryService;

    @RequestMapping("/list")
    public String list(
            @RequestParam(value = "navtabid", required = false) String navtabid,
            Model model) {
       // model.addAttribute("list",productCategoryService.getAllList());
        model.addAttribute("navtabid", navtabid);
        return "product/category/list";
    }


    @RequestMapping("/list1")
    public String list1(
            @RequestParam(value = "navtabid", required = false) String navtabid,
            Model model) {
        // model.addAttribute("list",productCategoryService.getAllList());
        model.addAttribute("navtabid", navtabid);
        return "product/category/list1";
    }

    @ResponseBody
    @RequestMapping("/dotree")
    public List<ProductCategory> dotree() {
        return productCategoryService.getAllList();
    }




    @ResponseBody
    @RequestMapping("/asynctree")
    public List<ProductCategory> asynctree(@RequestParam(value = "id", required = false) Long id) {
        List<ProductCategory> list= productCategoryService.listSub(id);
        return  list;
    }

    @RequestMapping("/tab/{id}")
    public String tab(
            @PathVariable("id") Long id,
            @RequestParam(value = "navtabid", required = false) String navtabid,
            Model model) {
        ProductCategory productCategory=productCategoryService.selectProductCategory(id);
        model.addAttribute("category", productCategory);  //此节点信息
        List<ProductCategory>  list=productCategoryService.listSub(id);//获得此节点的所有子节点
        model.addAttribute("navtabid", navtabid);
        model.addAttribute("list", list);
        return "product/category/tab";
    }

    @ResponseBody
    @RequestMapping("/edit")
    public AjaxDone update(@ModelAttribute("category")ProductCategory category,
                           @RequestParam("navtabid") String navtabid
    )
    {
        productCategoryService.updateProductCategory(category);
        AjaxDone a=new AjaxDone();
        a.setStatusCode("200");
        a.setMessage("更新成功");
        a.setNavTabId(navtabid);
        return a;
    }


    @ResponseBody
    @RequestMapping("/add")
    public AjaxDone add(@ModelAttribute("category")ProductCategory category,
                           @RequestParam("navtabid") String navtabid
    )
    {
        productCategoryService.insertProductCategory(category);
        AjaxDone a=new AjaxDone();
        a.setStatusCode("200");
        a.setMessage("更新成功");
        a.setNavTabId(navtabid);
        return a;
    }



    @ResponseBody
    @RequestMapping("/delete/{id}")
    public AjaxDone delete(@PathVariable("id") Long id,
                           @RequestParam("navtabid") String navtabid) {
        productCategoryService.deleteProductCategory(id);
        AjaxDone a = new AjaxDone();
        a.setStatusCode("200");
        a.setMessage("删除成功");
        a.setNavTabId(navtabid);
        return a;
    }

    @ResponseBody
    @RequestMapping("/eidtindexs")
    public AjaxDone eidtindexs( HttpServletRequest request,
                           @RequestParam("navtabid") String navtabid) {
        String [] ids=request.getParameterValues("id");
        String [] indexs=    request.getParameterValues("indexs");
        productCategoryService.updateIndexs(ids,indexs);
        AjaxDone a = new AjaxDone();
        a.setStatusCode("200");
        a.setMessage("保存索引成功");
        a.setNavTabId(navtabid);
        return a;
    }

}
